layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'form'], function(){
    var $ = layui.$, form = layui.form;

    var id = $('#id').val();
    var data = {'id': id};
    post('banner/toEdit', data, false,function (data) {
        var banner = data.banner;
        $('#name').val(banner.name);
        if(banner.img != null){
            $('#iconImg').attr('src', ossDomain + '/' + banner.img);
            $('#img').val(banner.img);
        }
        if(banner.type == 0){
            $('input[name=type][value="0"]').attr('checked', true);
        }else if(banner.type == 1){
            $('input[name=type][value="1"]').attr('checked', true);
        }else if(banner.type == 2){
            $('input[name=type][value="2"]').attr('checked', true);
        }
        $('#url').val(banner.url);
        $('#orderId').val(banner.orderId);
        form.render();
    });

    //加载上传控件
    upload("iconDiv", function (up, file, info) {
        var option = up.getOption();
        var host = option.url;
        var dir = option.multipart_params.key;

        var url = host + '/' + dir;
        $('#iconImg').attr('src', url);
        $('#img').val(dir);
        $('#iconDel').show();
    });

    //删除图片
    $('#iconDel').on('click', function () {
        $('#iconImg').removeAttr('src');
        $('#img').val('');
        $('#iconDel').hide();
    });

    //校验
    form.verify({
        'img-verify': function (value, item) {
            var imgSrc = $('#iconImg').attr("src")
            if(imgSrc == '' || imgSrc == undefined){
                return '请上传banner图片';
            }
        },
        'type-verify': function (value, item) {
            var type = $('input[name=type]:checked').val();
            if(type == undefined || type.trim() == ''){
                return '请选择banner类型';
            }
        }
    });
});